﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using GSMock;
using System.Xml;
using System.Collections.Specialized;

namespace GSMock.UnitTests
{
    [TestClass]
    public class ResponseTransformerTests
    {
        private XmlDocument baseXmlDocument;
        private XmlDocument BaseXmlDocument
        {
            get
            {
                if (this.baseXmlDocument == null)
                {
                    this.baseXmlDocument = new XmlDocument();
                    this.baseXmlDocument.LoadXml(
                        @"<?xml version='1.0' encoding='UTF-8' standalone='no'?>
                          <GSP VER='3.2'>
                            <TM>1.234567</TM>
                            <Q>test</Q>
                            <PARAM name='site' value='test' />
                            <PARAM name='p1' value='v1' />
                            <PARAM name='p2' value='v2' />
                          </GSP>");
                }
                return this.baseXmlDocument;
            }
        }

        /// <summary>
        ///   Tests parameter addition through the results transformer.
        /// </summary>
        [TestMethod]
        public void ParameterAdditionTest()
        {
            NameValueCollection parameters = new NameValueCollection();
            parameters.Add("p3", "v3");

            ResultsTransformer rt = new ResultsTransformer(this.BaseXmlDocument, parameters);
            XmlDocument result = rt.Transform();

            string xpath = "GSP/PARAM[@name='p3']/@value";

            Assert.IsTrue(this.TextXPathExpression(result, xpath, "v3"));
        }

        /// <summary>
        ///   Tests parameter update through the results transformer
        /// </summary>
        [TestMethod]
        public void UpdateParameterTest()
        {
            NameValueCollection parameters = new NameValueCollection();
            parameters.Add("p1", "v1.1");

            ResultsTransformer rt = new ResultsTransformer(this.BaseXmlDocument, parameters);
            XmlDocument result = rt.Transform();

            string xpath = "GSP/PARAM[@name='p1']/@value";

            Assert.IsTrue(this.TextXPathExpression(result, xpath, "v1.1"));
        }

        /// <summary>
        ///   Tests that a revered (ignored) parameter is not updated.
        /// </summary>
        [TestMethod]
        public void UpdateReserverParameterTest()
        {
            NameValueCollection parameters = new NameValueCollection();
            parameters.Add("site", "my_site");

            ResultsTransformer rt = new ResultsTransformer(this.BaseXmlDocument, parameters);
            XmlDocument result = rt.Transform();

            string xpath = "GSP/PARAM[@name='site']/@value";

            Assert.IsTrue(this.TextXPathExpression(result, xpath, "test"));
        }

        private bool TextXPathExpression(XmlDocument xml, string xpath, string expectedValue)
        {
            System.Xml.XPath.XPathNavigator nav = xml.CreateNavigator();
            var node = nav.SelectSingleNode(xpath);

            if (node != null)
            {
                return node.Value == expectedValue;
            }
            else
            {
                return false;
            }
        }
    }
}
